/**
 * Copyright (c) Daniel Centore
 *
 * ex: set filetype=java expandtab tabstop=4 shiftwidth=4 :
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation.
 */
package ronp.anim.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JViewport;

import ronp.anim.data.filmstrip.Animation;
import ronp.anim.gui.strip.BottomPanel;
import ronp.anim.gui.tools.AnimMenu;
import ronp.anim.gui.tools.ObjectsMenu;
import ronp.anim.gui.tools.ToolsMenu;
import ronp.anim.gui.viewer.AnimViewer;
import ronp.anim.kludges.RepaintListener;
import ronp.anim.undo.UndoController;

/**
 * The main frame we are going to work in
 * 
 * @author Daniel Centore
 */
public class Gui extends JFrame implements RepaintListener
{
	private static final long serialVersionUID = 1L;

	private static int guiCount = 0; // how many gui's have been initialized?

	private final UndoController undoController;

	/**
	 * Initializes the GUI
	 */
	public Gui(Animation anim)
	{
		super("RONP Animation Creator");

		guiCount++;

		undoController = new UndoController(anim);

		anim.addListener(this);

		this.setSize(1024, 768);
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

		SelectedState state = new SelectedState();

		AnimViewer viewer = new AnimViewer(anim, state, this);
		JViewport viewport = new JViewport();
		viewport.setView(viewer);
		viewport.setBackground(Color.black);

		JPanel top = new JPanel();
		top.setLayout(new BorderLayout());
		JTabbedPane pane = new JTabbedPane();

		final ToolsMenu tools = new ToolsMenu(viewer, anim, state);
		tools.setPreferredSize(new Dimension(50, 500));

		this.addWindowListener(new WindowListener()
		{
			@Override
			public void windowClosing(WindowEvent arg0)
			{
				Object[] options = { "Save", "Discard Changes", "Cancel" };
				int n = JOptionPane.showOptionDialog(Gui.this, "You wanna save changes?", "Hm?", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);

				switch (n)
				{
				case 0:
					tools.saveAction();
				case 1:
					guiCount--;
					Gui.this.dispose();

					if (guiCount <= 0)
						System.exit(0);
					break;

				case 2:
					return;
				}
			}

			@Override
			public void windowOpened(WindowEvent arg0)
			{
				// useless
			}

			@Override
			public void windowIconified(WindowEvent arg0)
			{
				// useless
			}

			@Override
			public void windowDeiconified(WindowEvent arg0)
			{
				// useless
			}

			@Override
			public void windowDeactivated(WindowEvent arg0)
			{
				// useless
			}

			@Override
			public void windowClosed(WindowEvent arg0)
			{
				// useless
			}

			@Override
			public void windowActivated(WindowEvent arg0)
			{
				// useless
			}
		});

		// JColorChooser chooser = new JColorChooser(Color.black);

		pane.addTab("Tools", tools);
		pane.addTab("Objects", new JScrollPane(new ObjectsMenu(viewer, anim, state)));
		// pane.addTab("Color Chooser", chooser);

		JSplitPane topPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, viewport, pane);
		topPane.setResizeWeight(1.0);
		top.add(topPane);

		JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, top, new BottomPanel(anim, state, this));
		split.setResizeWeight(1.0);

		this.add(split, BorderLayout.CENTER);

		this.add(new AnimMenu(viewer, anim, state, this), BorderLayout.NORTH);

		this.setVisible(true);
	}

	public UndoController getUndoController()
	{
		return undoController;
	}

}
